package com.lanyan.system.controller.system;

import java.math.BigDecimal;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.common.utils.http.HttpUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.lanyan.system.domain.Estate;
import com.lanyan.system.service.IEstateService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 小区Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/system/estate")
public class EstateController extends BaseController
{
    @Autowired
    private IEstateService estateService;

    /**
     * 查询小区列表
     */
    @PreAuthorize("@ss.hasPermi('system:estate:list')")
    @GetMapping("/list")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "mainImg", value = "主图", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "address", value = "地址", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "longitude", value = "经度", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "latitude", value = "纬度", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
          @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),

    })
    public TableDataInfo list(Estate estate)
    {
        startPage();
        List<Estate> list = estateService.selectEstateList(estate);
        return getDataTable(list);
    }

    /**
     * 导出小区列表
     */
    @PreAuthorize("@ss.hasPermi('system:estate:export')")
    @Log(title = "小区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Estate estate)
    {
        List<Estate> list = estateService.selectEstateList(estate);
        ExcelUtil<Estate> util = new ExcelUtil<Estate>(Estate.class);
        util.exportExcel(response, list, "小区数据");
    }

    /**
     * 获取小区详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:estate:query')")
    @GetMapping(value = "/{estateId}")
    public AjaxResult getInfo(@PathVariable("estateId") Long estateId)
    {
        return success(estateService.selectEstateByEstateId(estateId));
    }

    /**
     * 新增小区
     */
    @PreAuthorize("@ss.hasPermi('system:estate:add')")
    @Log(title = "小区", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "mainImg", value = "主图", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "address", value = "地址", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "longitude", value = "经度", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "latitude", value = "纬度", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult add(@RequestBody Estate estate)
    {
        getLatLon(estate);
        return toAjax(estateService.insertEstate(estate));
    }

    /**
     * 修改小区
     */
    @PreAuthorize("@ss.hasPermi('system:estate:edit')")
    @Log(title = "小区", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "mainImg", value = "主图", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "address", value = "地址", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "longitude", value = "经度", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "latitude", value = "纬度", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult edit(@RequestBody Estate estate)
    {
        getLatLon(estate);
        return toAjax(estateService.updateEstate(estate));
    }

    /**
     * 删除小区
     */
    @PreAuthorize("@ss.hasPermi('system:estate:remove')")
    @Log(title = "小区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{estateIds}")
    public AjaxResult remove(@PathVariable Long[] estateIds)
    {
        return toAjax(estateService.deleteEstateByEstateIds(estateIds));
    }

    public Estate getLatLon(Estate estate){
        String params = StringUtils.format("key={}&address={}", "4G3BZ-NRO6B-RMUUJ-JATF4-LNJTF-FLB62", StringUtils.trim(estate.getAddress()));
        String res = HttpUtils.sendGet("https://apis.map.qq.com/ws/geocoder/v1/?"+params);
        JSONObject resData = JSONObject.parseObject(res);
        if (resData.getInteger("status") == 0){
            JSONObject result = resData.getJSONObject("result");
            JSONObject location = result.getJSONObject("location");
            estate.setLatitude(location.getString("lat"));
            estate.setLongitude(location.getString("lng"));
        }
        return estate;
    }
}
